package alo;

public class L_InsertSort {
    public static void main(String[] args) {
        double[] arr = {2.0, 2.1, 9, 5.02, 6, 3.04, 8, 4, 0.3, 5, 3.22, 6.2};
        sort(arr);
        for (double v : arr) {
            System.out.print(" " + v);
        }
    }

    private static void sort(double[] data) {
        int len = data.length;
        for (int i = 1; i < len; i++) {
            int j = i;
            double tmp = data[i];
            while (j > 0) {
                //找到合适的插入下标位置
                if (data[j - 1] < tmp) {
                    break;
                }
                j--;//往左继续找比它小的值
            }
            //把元素后移
            for (int k = i; k > j; k--)
                data[k] = data[k - 1];
            data[j] = tmp;
        }
    }
}
